package cn.janescott.framework.core.utils;

import cn.janescott.framework.core.annotation.RequiredProperty;

import java.lang.reflect.Field;

/**
 * 注解工具类
 *
 * @author scott
 * @date 2019/2/18
 */
public final class AnnotationUtils {

    /**
     * 检查{@see Required}Annotation
     * @param targetClass
     * @param targetObj
     * @throws IllegalAccessException
     */
    public static void checkRequired(Class targetClass, Object targetObj) throws IllegalAccessException {
        Field[] declaredFields = targetClass.getDeclaredFields();
        for (Field field : declaredFields) {
            RequiredProperty annotation = field.getAnnotation(RequiredProperty.class);
            if (null != annotation) {
                field.setAccessible(true);
                Object target = field.get(targetObj);
                if (null == target) {
                    throw new RuntimeException(annotation.value() + " is required!");
                }
            }
        }
    }


}
